
import os
from DataSheetClass import Sheet,DataSheet
from CsvDatas import CsvDatas
class DataBase:
    def __init__(self, filepath):
        self.dataintface = None
        self.ext = ""
        self.filepath = filepath
        self.rootpath = os.path.dirname(filepath)
        if filepath and os.path.exists(filepath):
            self.datasheet = DataSheet(filename=filepath)
            self.ext = os.path.splitext(os.path.basename(self.datasheet.source_file_path))[1]
            if self.ext == ".csv":
                self.dataintface = CsvDatas(self.datasheet)
                self.read_source()
                if len(self.datasheet.Sheets) > 0 :
                    self.datasheet.current_sheet = self.datasheet.Sheets[0]
                
        else :
            raise ValueError(f"filepath not right : {filepath}")
    
    def read_source(self):
        if self.dataintface :
            return self.dataintface.read_source()
        else:
            raise NotImplementedError("File {self.datasheet.source_file_path} ext {self.ext} have no datainterface")

    def update_source(self):
        if self.dataintface :
            return self.dataintface.update_source()
        else:
            raise NotImplementedError("File {self.datasheet.source_file_path} ext {self.ext} have no datainterface")
       
    def view(self):
        if self.dataintface :
            return self.dataintface.view()
        else:
            raise NotImplementedError("File {self.datasheet.source_file_path} ext {self.ext} have no datainterface")
    
    